package com.lemon.cloud.websocket.distribute;

import com.lemon.cloud.websocket.config.WebSocketMessageSender;
import com.lemon.cloud.websocket.message.MessageDO;
import org.springframework.util.CollectionUtils;

import java.util.List;

public interface MessageSender {
    /**
     * 发送消息
     * @param messageDO
     */
    default void doSend(MessageDO messageDO) {
        Boolean needBroadcast = messageDO.getNeedBroadcast();
        String messageText = messageDO.getMessageText();
        List<Object> sessionKeys = messageDO.getSessionKeys();
        if (needBroadcast != null && needBroadcast) {
            WebSocketMessageSender.broadcast(messageText);
        } else if (!CollectionUtils.isEmpty(sessionKeys)) {
            sessionKeys.forEach(item->{
                WebSocketMessageSender.send(item, messageText);
            });
        }

    }
}
